package com.ruoyi.web.controller.regulator.stats;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.regulator.domain.pojo.Organization;
import com.ruoyi.regulator.domain.pojo.NjtRegionInfo;
import com.ruoyi.regulator.service.OrganizationService;
import com.ruoyi.regulator.service.NjtProvincesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nongjt/organization")
public class NjtOrganizationController extends BaseController {
    @Autowired
    private OrganizationService njtOrganizationService;

    @Autowired
    private NjtProvincesService njtProvincesService;

    /**
     * 全查询监管者单位列表
     */
    @GetMapping("/all")
    public TableDataInfo list(Organization organization) {
        startPage();
        List<Organization> list = njtOrganizationService.getNjtOrganizationList(organization);
        fillAreaNames(list);
        return getDataTable(list);
    }

    /**
     * 根据省市区查询监管者单位列表
     */
    @GetMapping("/list")
    public TableDataInfo listByArea(Organization organization) {
        startPage();
        List<Organization> list = njtOrganizationService.getNjtOrganizationList(organization);
        fillAreaNames(list);
        return getDataTable(list);
    }

    /**
     * 填充省市区名称
     */
    private void fillAreaNames(List<Organization> list) {
        for (Organization org : list) {
            // 查询省名称
            if (org.getProvinceId() != null) {
                NjtRegionInfo provinceQuery = new NjtRegionInfo();
                provinceQuery.setProvinceId(org.getProvinceId());
                List<NjtRegionInfo> provinceList = njtProvincesService.getRegionInfoList(provinceQuery);
                if (!provinceList.isEmpty()) {
                    org.setProvinceName(provinceList.get(0).getProvinceName());
                }
            }

            // 查询市名称
            if (org.getCityId() != null) {
                NjtRegionInfo cityQuery = new NjtRegionInfo();
                cityQuery.setCityId(org.getCityId());
                List<NjtRegionInfo> cityList = njtProvincesService.getRegionInfoList(cityQuery);
                if (!cityList.isEmpty()) {
                    org.setCityName(cityList.get(0).getCityName());
                }
            }

            // 查询区名称
            if (org.getDistrictId() != null) {
                NjtRegionInfo districtQuery = new NjtRegionInfo();
                districtQuery.setDistrictId(org.getDistrictId());
                List<NjtRegionInfo> districtList = njtProvincesService.getRegionInfoList(districtQuery);
                if (!districtList.isEmpty()) {
                    org.setDistrictName(districtList.get(0).getDistrictName());
                }
            }
        }
    }

    /**
     * 修改监管者单位信息
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Organization organization) {
        return toAjax(njtOrganizationService.updateNjtOrganization(organization));
    }

    /**
     * 删除监管者单位信息
     */
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        return toAjax(njtOrganizationService.deleteNjtOrganizationById(id));
    }

    /**
     * 新增监管者单位信息
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Organization organization) {
        return toAjax(njtOrganizationService.insertNjtOrganization(organization));
    }
}